/*
 * (C) Copyright 2016
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/*
 * RTC, Date & Time support: get and set date & time
 */
#include <common.h>
#include <command.h>

DECLARE_GLOBAL_DATA_PTR;

#ifdef LS1X_CMD_UPDATE_USB
static int updatedelay_normal(int updatedelay)
{
	int abort = 0;
	unsigned long ts;

	printf("Hit any key to stop update: %2d ", updatedelay);

	while ((updatedelay > 0) && (!abort)) {
		--updatedelay;
		/* delay 1000 ms */
		ts = get_timer(0);
		do {
			if (tstc()) {	/* we got a key press	*/
				abort  = 1;	/* don't auto boot	*/
				updatedelay = 0;	/* no more delay	*/
				(void) getc();  /* consume input	*/
				break;
			}
			udelay(10000);
		} while (!abort && get_timer(ts) < 1000);

		printf("\b\b\b%2d ", updatedelay);
	}

	putc('\n');

	return abort;
}

static int do_update_usb(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	int ret;
	char *filesize;
	char cmd_buf[80];

	run_command("usb start", 0);

	ret = run_command("usb storage", 0);
	if (ret == 0) {
		if (updatedelay_normal(2))
			return 0;

		printf("update u-boot .............\n");
		ret = run_command("fatload usb 0:1 80100000 update/u-boot.bin", 0);
		if (ret == 0) {
			run_command("sf probe 0;sf erase 0 80000;sf update 80100000 0 ${filesize}", 0);
		}

		printf("update kernel .............\n");
		ret = run_command("fatload usb 0:1 80100000 update/uImage", 0);
		if (ret == 0) {
			run_command("nand erase.part kernel;nand write 80100000 kernel ${filesize}", 0);
		}

		printf("update rootfs .............\n");
		ret = run_command("fatload usb 0:1 80100000 update/rootfs-yaffs2.img", 0);
		if (ret == 0) {
			run_command("nand erase.part root;nand write.yaffs 80100000 root ${filesize}", 0);
#ifdef CONFIG_LS1B_ACCESS_BOARD
			run_command("setenv bootargs console=ttyS5,115200 root=/dev/mtdblock1 noinitrd rootfstype=yaffs2 rw video=ls1xfb:480x272-16@60", 0);
#else
			run_command("setenv bootargs console=ttyS2,115200 root=/dev/mtdblock1 noinitrd rootfstype=yaffs2 rw video=ls1xfb:480x272-16@60", 0);
#endif
			run_command("setenv bootcmd nboot kernel\\;bootm 82000000", 0);
			run_command("saveenv", 0);
		} else if (run_command("fatload usb 0:1 80100000 update/rootfs-cramfs.img", 0) == 0) {
			run_command("nand erase.part root;nand write 80100000 root ${filesize}", 0);
#ifdef CONFIG_LS1B_ACCESS_BOARD
			run_command("setenv bootargs console=ttyS5,115200 root=/dev/mtdblock1 noinitrd rootfstype=cramfs video=ls1xfb:480x272-16@60", 0);
#else
			run_command("setenv bootargs console=ttyS2,115200 root=/dev/mtdblock1 noinitrd rootfstype=cramfs video=ls1xfb:480x272-16@60", 0);
#endif
			run_command("setenv bootcmd nboot kernel\\;bootm 82000000", 0);
			run_command("saveenv", 0);
		} else if (run_command("fatload usb 0:1 80100000 update/rootfs-ubifs-ze.img", 0) == 0) {
			run_command("nand erase.part root;nand write 80100000 root ${filesize}", 0);
#ifdef CONFIG_LS1B_ACCESS_BOARD
			run_command("setenv bootargs console=ttyS5,115200 root=ubi0:rootfs ubi.mtd=root,2048 noinitrd rootfstype=ubifs rw video=ls1xfb:480x272-16@60", 0);
#else
			run_command("setenv bootargs console=ttyS2,115200 root=ubi0:rootfs ubi.mtd=root,2048 noinitrd rootfstype=ubifs rw video=ls1xfb:480x272-16@60", 0);
#endif
			run_command("setenv bootcmd nboot kernel\\;bootm 82000000", 0);
//			run_command("setenv bootcmd ubi part root 2048\\;ubifsmount ubi:rootfs\\;ubifsload 82000000 uImage\\;bootm 82000000", 0);
			run_command("saveenv", 0);
		}
	}

	return ret;
}

/***************************************************/

U_BOOT_CMD(
	update_usb,	1,	1,	do_update_usb,
	"update uboot.bin kernel rootfs",
	"update uboot.bin uIamge and rootfs through usb"
);
#endif

#ifdef LS1X_CMD_UPDATE_MMC
static int do_update_mmc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
{
	int ret;
	char cmd_buf[20];

	sprintf(cmd_buf, "mmc_spi %d:%d", CONFIG_MMC_SPI_BUS, CONFIG_MMC_SPI_CS);
	ret = run_command(cmd_buf, 0);
	if (ret != 0)
		return ret;

	ret = run_command("fatls mmc 0:1", 0);
	if (ret != 0)
		return ret;

	printf("update u-boot .............\n");
	ret = run_command("fatload mmc 0:1 80100000 update/u-boot.bin", 0);
	if (ret == 0) {
		run_command("sf probe 0;sf erase 0 80000;sf update 80100000 0 ${filesize}", 0);
	}

	printf("update kernel .............\n");
	ret = run_command("fatload mmc 0:1 80100000 update/uImage", 0);
	if (ret == 0) {
		run_command("nand erase.part kernel;nand write 80100000 kernel ${filesize}", 0);
	}

	printf("update rootfs ubifs .............\n");
	ret = run_command("fatload mmc 0:1 80100000 update/rootfs-ubifs-ze.img", 0);
	if (ret == 0) {
		run_command("nand erase.part root;nand write 80100000 root ${filesize}", 0);
	}

	return ret;
}

/***************************************************/

U_BOOT_CMD(
	update_mmc,	1,	1,	do_update_mmc,
	"update uboot.bin kernel rootfs",
	"update uboot.bin uIamge and rootfs through mmc"
);
#endif
